package com.luntan.util.web;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.springframework.util.Assert;

/**
 * 日期转换工具类
 *
 */
public class DateUtil {

    public static final String YYYYMM = "yyyy/MM";
    public static final String YYYY_MM = "yyyy-MM";
    public static final String detailFmt = "yyyy/MM/dd hh:mm:ss";
    public static final String detailFmt1 = "yyyy/MM/dd HH:mm:ss";
    public static final String simplyFmt = "yyyy/MM/dd";
    public static final String dogFmt = "yyyyMMdd";
    public static final String simplyFmt1 = "yyyy-MM-dd";
    public static final String sqlFmt = "yyyy-MM-dd HH:mm:ss";

    public DateUtil() {
    }

    /**
     * 日期对象转换成字符串(yyyy-MM-dd HH:mm:ss)
     * @param date
     * @return 
     */
    public static String dateToSqlStr(Date date) {
        String myDate = null;
        SimpleDateFormat sdf = new SimpleDateFormat(sqlFmt);
        try {
            myDate = sdf.format(date);
        } catch (Exception e) {
        }
        return myDate;
    }

    /**
     * 日期对象转换成字符日期(yyyy/MM/dd)
     * @param date
     * @return 
     */
    public static String dateToSimplyStr(Date date) {
        String myDate = null;
        SimpleDateFormat sdf = new SimpleDateFormat(simplyFmt);
        try {
            myDate = sdf.format(date);
        } catch (Exception e) {
        }
        return myDate;
    }

    /**
     * 日期对象转换成字符日期(yyyyMMdd)
     * @param date
     * @return 
     */
    public static String dateToDogStr(Date date) {
        String myDate = null;
        SimpleDateFormat sdf = new SimpleDateFormat(dogFmt);
        try {
            myDate = sdf.format(date);
        } catch (Exception e) {
        }
        return myDate;
    }

    /**
     * 日期字符串(yyyy/MM/dd)转换成日期字符串(yyyy-MM-dd)
     * @param strDate
     * @return 
     */
    public static String strToSimplyStr(String strDate) {
        String myDate = null;
        Date date = StrToDate(strDate);
        SimpleDateFormat sdf = new SimpleDateFormat(simplyFmt1);
        try {
            myDate = sdf.format(date);
        } catch (Exception e) {
        }
        return myDate;
    }

    /**
     * 日期对象转换成字符日期(yyyy-MM-dd)
     * @param date
     * @return 
     */
    public static String dateSimplyStr(Date date) {
        String myDate = null;

        SimpleDateFormat sdf = new SimpleDateFormat(simplyFmt1);
        try {
            myDate = sdf.format(date);
        } catch (Exception e) {
        }
        return myDate;
    }

    /**
     * 日期对象转换成字符日期(yyyy-MM)
     * @param Month
     * @return 
     */
    public static String strToSimplyMonth(String Month) {
        String myDate = null;
        Date date = StrToMonth(Month);
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM);
        try {
            myDate = sdf.format(date);
        } catch (Exception e) {
        }
        return myDate;
    }
    
    /**
     * 根据年月生成年月字符串，格式yyyy/MM,
     * <p>
     * eg: formatMonth(2011,1),返回2011/01
     *
     * @param year
     * @param month
     * @return
     */
    public static String formatMonth(Integer year, Integer month) {
        Assert.isTrue(year != null, "year不能为空");
        Assert.isTrue(month != null, "month不能为空");
        Assert.isTrue(year >= 1950 && year < 2050, "年份必须属于[1950,2050)");
        Assert.isTrue(month >= 1 && month <= 12, "月份必须数据[1,12]");

        return year + "/" + (month > 9 ? month : "0" + month);
    }

    /**
     * 将日期对象转换成字符串(yyyy/MM/dd hh:mm:ss)
     * @param date
     * @return 
     */
    public static String dateToDetailStr(Date date) {
        String myDate = null;
        SimpleDateFormat sdf = new SimpleDateFormat(detailFmt);
        try {
            myDate = sdf.format(date);
        } catch (Exception e) {
        }
        return myDate;
    }

    /**
     * 将日期对象转换成字符串(yyyy/MM/dd HH:mm:ss)
     * @param date
     * @return 
     */
    public static String dateToDetailStr1(Date startDate) {
        String myDate = null;
        SimpleDateFormat sdf = new SimpleDateFormat(detailFmt1);
        try {
            myDate = sdf.format(startDate);
        } catch (Exception e) {
        }
        return myDate;
    }

    /**
     * 日期字符串(yyyy/MM/dd)转换成Date类型对象
     * @param startDate
     * @return 
     */
    public static Date StrToDate(String startDate) {
        Date myDate = null;
        SimpleDateFormat df = new SimpleDateFormat(simplyFmt);

        try {
            myDate = df.parse(startDate);
        } catch (Exception e) {
            // e.printStackTrace(System.out);
        }

        return myDate;
    }

    /**
     * 字符串月份转换成带年月的日期对象
     * @param Month
     * @return 
     */
    public static Date StrToMonth(String Month) {
        Date myDate = null;
        SimpleDateFormat df = new SimpleDateFormat(YYYYMM);

        try {
            myDate = df.parse(Month);
        } catch (Exception e) {
            // e.printStackTrace(System.out);
        }

        return myDate;
    }

    /**
     * 日期字符串(yyyy-MM-dd)转换成Date类型对象
     * @param strDate
     * @return 
     */
    public static Date StrToDate1(String strDate) {
        Date myDate = null;
        SimpleDateFormat df = new SimpleDateFormat(simplyFmt1);

        try {
            myDate = df.parse(strDate);
        } catch (Exception e) {
            // e.printStackTrace(System.out);
        }

        return myDate;
    }

    /**
     * 日期时间字符串(yyyy/MM/dd hh:mm:ss)转换成Date类型对象
     * @param strDate
     * @return 
     */
    public static Date StrToDateEnd(String strDate) {
        Date myDate = null;
        SimpleDateFormat df = new SimpleDateFormat(detailFmt);

        try {
            myDate = df.parse(strDate + " 23:00:00");
        } catch (Exception e) {
            // e.printStackTrace(System.out);
        }

        return myDate;
    }

    /**
     * 将字符串日期(yyyy/MM/dd)往后追加一天
     * @param strDate
     * @return 
     */
    public static String afterDate(String strDate) {
        if (StringUtil.notNull(strDate).equals("")) {
            return "";
        }
        Calendar calendar = Calendar.getInstance();
        Date date = DateUtil.StrToDate(strDate);
        calendar.setTime(date);
        calendar.add(Calendar.DATE, 1);
        String s = DateUtil.dateToSimplyStr(calendar.getTime());
        return s;
    }

    /**
     * 判断String是否为Date格式
     *
     *  Date:2011-12-20
     */
    public static Boolean isDate(String strDate) {
        SimpleDateFormat sdf = new SimpleDateFormat(simplyFmt1);
        Boolean b = true;
        try {
            sdf.parse(strDate);
        } catch (Exception e) {
            b = false;
        } finally {
            return b;
        }
    }

    /**
     * 计算两个时间之间相隔天数sd
     * @param startday
     * @param endday
     * @return 
     */
    public static int getIntervalDays(Date startday, Date endday) {
        // 确保startday在endday之前
        if (startday.after(endday)) {
            Date cal = startday;
            startday = endday;
            endday = cal;
        }
        // 分别得到两个时间的毫秒数
        long sl = startday.getTime();
        long el = endday.getTime();

        long ei = el - sl;
        // 根据毫秒数计算间隔天数
        return (int) (ei / (1000 * 60 * 60 * 24));

    }
    
    /**
     * 将一个日期对象的年部分返回
     * @param date
     * @return 
     */
    public static String getYYYY(Date date){
        if(date==null){
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        return sdf.format(date);
    }

    /**
     * 将一个日期对象的月部分返回
     * @param date
     * @return 
     */
    public static String getMM(Date date){
        if(date==null){
            return "";
        }        
        SimpleDateFormat sdf = new SimpleDateFormat("MM");
        return sdf.format(date);
    }

    /**
     * 将一个日期对象的日部分返回
     * @param date
     * @return 
     */
    public static String getDD(Date date){
        if(date==null){
            return "";
        }        
        SimpleDateFormat sdf = new SimpleDateFormat("dd");
        return sdf.format(date);
    }
    
    public static void main(String[] args) {
        Date date =new Date();
        
        System.out.println(DateUtil.getYYYY(date));
        System.out.println(DateUtil.getMM(date));
        System.out.println(DateUtil.getDD(date));
        
    }
}
